﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using DBAccessLib;

namespace LineWatch_Web
{
    public partial class PositionManagement : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindList();//显示当天所有流水线
                if (ddlLine.Items.Count > 0)
                {
                    int i = 0;
                    //GetInteval();
                    if (Request["line"] != null)
                    {
                        string line = Request["line"].ToString();
                        i = ddlLine.Items.IndexOf(ddlLine.Items.FindByValue(line));
                        ddlLine.SelectedIndex = i;
                        hidLine.Value = line;
                    }
                    BindData(ddlLine.Items[i].Value.ToString());//显示数据
                    GetInteval();
                }
            }
        }

        private void GetInteval()
        {
            DataRow row = Product.GetLineWatchRecord(ddlLine.SelectedValue, "ScreenT");
            if (row != null)
            {
                hidTime.Value = row["Content2"].ToString().Trim();
                if (hidTime.Value.Equals("")) hidTime.Value = "5";
            }
            else hidTime.Value = "5";
        }

        private void BindData(string lineCode)
        {
            List<string> helpPositions = Product.GetHelpPositionInfo(lineCode);
            List<string> stopPositions = Product.GetStopPositionInfo(lineCode);
            List<string> alarmPositions = Product.GetAlarmPositionInfo(lineCode);
            if (helpPositions.Count > 0) labHelp.Text = string.Join("、", helpPositions);
            else labHelp.Text = "无求助工位 :)";
            if (stopPositions.Count > 0) labStop.Text = string.Join("、", stopPositions);
            else labStop.Text = "无紧停工位 :)";
            if (alarmPositions.Count > 0) labAlarm.Text = string.Join("、", alarmPositions);
            else labAlarm.Text = "无异常工位 :)";

            hidLine.Value = lineCode;
        }

        private void BindList()
        {
            DataTable dt = Product.GetAllLines();
            ddlLine.Items.Clear();
            foreach (DataRow row in dt.Rows)
            {
                ListItem li = new ListItem(row["Line_Name"].ToString().Trim(), row["Line_Code"].ToString().Trim());
                ddlLine.Items.Add(li);
            }
        }

        protected void btnView_Click(object sender, EventArgs e)
        {
            if (ddlLine.Items.Count > 0)
            {
                hidLine.Value = ddlLine.SelectedValue;
                BindData(ddlLine.SelectedItem.Value.ToString());
            }
        }
    }
}